<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
    p,
    body {
        margin: 0;
    }

    p {
        width: 150px;
    }

    #app {
        height: 200px;
        width: 300px;
        overflow-y: scroll;
        border: 1px solid #ccc;

    }

    p:nth-child(1) {
        height: 500px;
    }

    p:nth-child(2) {
        color: #ccc;
        font-weight: bold;
    }
</style>

<body>
    <div id="app">
        <p>向下滚动页面</p>
        <p v-pin:[direction]="100">粘在页面顶部100px</p>
    </div>
</body>

</html>
<script>

    Vue.directive('pin', {
        inserted(el, binding) {
            // console.log(binding);
            let d = binding.arg;
            console.log(d);
            el.style.position = "fixed"
            el.style[d] = binding.value + "px"
        },
        update(el, binding) {
            let d = binding.arg;
            console.log(d);
            el.style = {}
            el.style.position = "fixed"
            el.style[d] = binding.value + "px"
        }
    })

    const vm = new Vue({
        el: "#app",
        data: {
            direction: 'top'
        }
    })
</script>